package com.thti.mecool.client.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.threeti.mecool.mobile.interfaces.dto.ProjectWithStoresAndSellIntoDto;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.command.ManageCommand;
import com.thti.mecool.client.command.result.sellinto.SellIntoCommand;
import com.thti.mecool.client.command.send.saleinto.SellIntoSend;
import com.thti.mecool.client.model.biz.SalersBizImpl;
import com.thti.mecool.client.model.biz.SalersProjectBizImpl;
import com.thti.mecool.client.model.biz.SalersStoreBizImpl;
import com.thti.mecool.client.model.po.Salers;
import com.thti.mecool.client.model.po.SalersProject;
import com.thti.mecool.client.model.po.SalersStore;
import com.thti.mecool.client.util.Util;
import com.thti.mecool.client.widget.ProgressDialogWidget;
import com.thti.mecool.client.widget.Toolbar;

public class SellAndReceiptActivity extends BaseActivity{
	private ArrayList<String> SellList;
	private ListView SellListView;
	List<ProjectWithStoresAndSellIntoDto> projwithstoresandsellinto;
	private List<SalersProject> salersProjects;
	ProgressDialogWidget progressDialogWidget;
	public static Handler handler;	
	SalersProjectBizImpl salersProjectBizImpl;
	SalersStoreBizImpl salersStoreBizImpl;
	SalersBizImpl salersBizImpl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		noTitle();
		setContentView(R.layout.mecool_sale_manage_first);
		initToolbar();
		init();
		handler=new Handler(){
			public void handleMessage(android.os.Message msg) {
				switch (msg.what) {
				case 0xAA:
					progressDialogWidget.dismiss();
					projwithstoresandsellinto=(List<ProjectWithStoresAndSellIntoDto>)msg.obj;
					salersProjects=getSalersProjectList(projwithstoresandsellinto);
					SellListView.setAdapter(SellAdapter);
					List<SalersStore> salersStores=getSalersStoreList(projwithstoresandsellinto);
					List<Salers> salersList=getSalersList(projwithstoresandsellinto);
//					insertInDataBase(salersProjects, salersStores,salersList);
//					List<SalersProject> list=salersProjectBizImpl.findAll(String.valueOf(getCurrentUser().getId()));
//					for(int i=0;i<list.size();i++){
//						System.out.println(list.get(i));
//					}
//					for(int i=0;i<salersStoreBizImpl.findAll(salersProjects.get(0).getProjectId()).size();i++){
//						System.out.println(salersStoreBizImpl.findAll(salersProjects.get(0).getProjectId()).get(i));
//					}
//					for(int i=0;i<salersBizImpl.findAll(salersList.get(0).getStoreId()).size();i++){
//						System.out.println(salersBizImpl.findAll(salersList.get(0).getStoreId()).get(i));
//					}
					break;
				case 0xAB:
					progressDialogWidget.dismiss();
					Util.sendToast(SellAndReceiptActivity.this, "无数据");
					break;
				default:
					break;
				}
			};
		};
		SellIntoCommand sellIntoCommand=new SellIntoCommand(handler);
		ManageCommand.addCommand(sellIntoCommand);
		requestData();
		service();
	}
	
	private void init(){
		SellListView=(ListView)findViewById(R.id.mecool_sale_manage_first_listview);
		salersProjectBizImpl=new SalersProjectBizImpl(this);
		salersStoreBizImpl=new SalersStoreBizImpl(this);
		salersBizImpl=new SalersBizImpl(this);
	}
	
	private void requestData(){
		/**
		 * test
		 */
		SellIntoSend.getSellInto(getCurrentUser().getLoginName());
		progressDialogWidget=new ProgressDialogWidget(SellAndReceiptActivity.this);
		progressDialogWidget.setDismissTime(16);
	}
	
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		toolbar.setToolbarCentreText("卖进管理");
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.hideRightButton();
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	}
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			finish();
		}};
	private OnItemClickListener onItemClickListener=new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent i=new Intent(SellAndReceiptActivity.this,SellStoreDetail.class);
			i.putExtra("ProjectStoreWithSellIntoDto", (Serializable)projwithstoresandsellinto.get(arg2).getProjStores());
			i.putExtra("item", arg2);
			startActivity(i);
		}
	};
	
	private void service(){
		SellListView.setOnItemClickListener(onItemClickListener);
	}
	
	private BaseAdapter SellAdapter = new BaseAdapter() {
		LinearLayout layout;
		TextView tv;
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			layout=new LinearLayout(SellAndReceiptActivity.this);
			layout.setPadding(10, 0, 10, 0);
			layout.setBackgroundResource(R.drawable.mecool_listview_selector);
			layout.setGravity(Gravity.CENTER_VERTICAL);
			tv=new TextView(SellAndReceiptActivity.this);
			tv.setTextColor(Color.BLACK);
			tv.setText(salersProjects.get(arg0).getName());
			layout.addView(tv);
			return layout;
		}
		
		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}
		
		@Override
		public Object getItem(int arg0) {
			return salersProjects.get(arg0);
		}
		
		@Override
		public int getCount() {
			return salersProjects.size();
		}
	};	
	
	private List<SalersProject> getSalersProjectList(List<ProjectWithStoresAndSellIntoDto> projwithstoresandsellinto){
		List<SalersProject> list=new ArrayList<SalersProject>();
		SalersProject salersProject=new SalersProject();
		for(int i=0;i<projwithstoresandsellinto.size();i++){
			salersProject.setName(projwithstoresandsellinto.get(i).getName());
			salersProject.setProjectId(projwithstoresandsellinto.get(i).getId());
			salersProject.setUserId(String.valueOf(getCurrentUser().getId()));
			list.add(salersProject);		
		}
		return list;
	}
	
	private List<SalersStore> getSalersStoreList(List<ProjectWithStoresAndSellIntoDto> projwithstoresandsellinto){
		List<SalersStore> storeList=new ArrayList<SalersStore>();
		for(int i=0;i<projwithstoresandsellinto.size();i++){
			for(int j=0;j<projwithstoresandsellinto.get(i).getProjStores().size();j++){
				SalersStore salersStore=new SalersStore();
				salersStore.setProjectId(projwithstoresandsellinto.get(i).getId());
				System.out.println(projwithstoresandsellinto.get(i).getProjStores().get(j).getId());
				salersStore.setStoreId(projwithstoresandsellinto.get(i).getProjStores().get(j).getId());
				salersStore.setName(projwithstoresandsellinto.get(i).getProjStores().get(j).getName());
				salersStore.setProvinceAdName(projwithstoresandsellinto.get(i).getProjStores().get(j).getProvinceAdName());
				salersStore.setProvinceAdCode(projwithstoresandsellinto.get(i).getProjStores().get(j).getProvinceAdCode());
				salersStore.setCityAdName(projwithstoresandsellinto.get(i).getProjStores().get(j).getCityAdName());
				salersStore.setCityAdCode(projwithstoresandsellinto.get(i).getProjStores().get(j).getCityAdCode());
				salersStore.setStatus(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getStatus());
				salersStore.setTgStockBaseDesc(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getTgStockBaseDesc());
				salersStore.setExecutionStatus(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getExecutionStatus());
				salersStore.setProblemType(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getProblemType());
				salersStore.setProblemDescription(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getProblemDescription());
				salersStore.setDescription(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getDescription());
				salersStore.setLastModified(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getLastModified());
				storeList.add(salersStore);
			}
			
		}
		return storeList;
	}
	
	
	private List<Salers> getSalersList(List<ProjectWithStoresAndSellIntoDto> projwithstoresandsellinto){
		List<Salers> salersList=new ArrayList<Salers>();
		for(int i=0;i<projwithstoresandsellinto.size();i++){
			for(int j=0;j<projwithstoresandsellinto.get(i).getProjStores().size();j++){
				for(int k=0;k<projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getSalesPromoters().size();k++){
					Salers salers=new Salers();
					salers.setStoreId(projwithstoresandsellinto.get(i).getProjStores().get(j).getId());
					salers.setIdCardNo(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getSalesPromoters().get(k).getIdCardNo());
					salers.setName(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getSalesPromoters().get(k).getName());
					salers.setOffTime(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getSalesPromoters().get(k).getOffTime());
					salers.setOnDutyTime(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getSalesPromoters().get(k).getOnDutyTime());
					salers.setPhone(projwithstoresandsellinto.get(i).getProjStores().get(j).getSellInto().getSalesPromoters().get(k).getPhone());	
					salersList.add(salers);
				}
			}
			
		}
		return salersList;
	}
	
	public void insertInDataBase(List<SalersProject> salersProjects,List<SalersStore> storeList,List<Salers> salersList){
		for(int i=0;i<salersProjects.size();i++){
			if(salersProjectBizImpl.findProjectId(String.valueOf(getCurrentUser().getId()), salersProjects.get(i).getProjectId())){
				salersProjectBizImpl.update(salersProjects.get(i));
			}else{
				salersProjectBizImpl.save(salersProjects.get(i));
			}
		}
		for(int i=0;i<storeList.size();i++){
			if(salersStoreBizImpl.findStoreId(storeList.get(i).getStoreId(), storeList.get(i).getProjectId())){
				salersStoreBizImpl.update(storeList.get(i));
			}else{
				salersStoreBizImpl.save(storeList.get(i));
			}
		}
		for(int i=0;i<salersList.size();i++){
			if(salersBizImpl.findSalerId(salersList.get(i).getStoreId(), salersList.get(i).getIdCardNo())){
				salersBizImpl.update(salersList.get(i));
			}else{
				salersBizImpl.save(salersList.get(i));
			}
		}
	}
	
	
}
